package eu.liquify.ui {
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	public class ScrollablePanel extends Panel implements ISimulatable {
		
		private var offset:Point = new Point();
		private var masker:Shape = new Shape();
		
		public function ScrollablePanel(options:Object = null) {
			super();
			fixedSize = false;
			applyOptions(options);
			
			backgroundDisplay.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			//addRawChild(masker);
			//mask = masker;
		}
		
		public function simulateEvent(e:Event):void {
			backgroundDisplay.dispatchEvent(e);
		}
		
		private function mouseDown(e:MouseEvent):void {
			TweenNano.killTweensOf(content);
			if (content.width <= width) return;
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			Mouse.cursor = MouseCursor.HAND;
			offset.x = e.stageX-content.x;
		}
		
		private function mouseMove(e:MouseEvent):void {
			content.x = e.stageX-offset.x;
		}
		
		private function mouseUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			Mouse.cursor = MouseCursor.AUTO;
			
			constrain();
		}
		
		override public function resize():void {
			super.resize();
			constrain();
		}
		
		public function constrain():void {
			var target:Number = NaN;
			if (content.x > padding) {
				target = padding;
			}
			if (content.x < width-(content.width+padding)) {
				target = width-(content.width+padding);
			}
			if (content.width <= width) target = padding;
			if (!isNaN(target)) {
				TweenNano.to(content, 0.2, { x: target } );
			}
		}
		
		override protected function redraw():void {
			super.redraw();
			
			/*
			var g:Graphics = masker.graphics;
			g.clear();
			g.beginFill(0xFF0000);
			g.drawRoundRect(0, 0, (fixedSize ? width : content.width+content.x*2)-1, (fixedSize ? height : content.height+content.y*2)-1, 6, 6);
			//*/
			
			scrollRect = new Rectangle(0, 0, width+1, height+1);
			
			//var m:Matrix = new Matrix();
			//m.createGradientBox(width, height);
			//g.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF], [1, 0, 0, 1], [0, 5, 250, 255], m);
			//g.drawRect(0, 0, width, height);
		}
		
	}

}